<%
function formatParams(params) {
  const items = _.pairs(params).reduce((str, [name, val]) => {
    const formatted = _.isObject(val) ? formatParams(val) : formatParamValue(val);
    return str + `<li><span class="name">${_.escape(name)}</span> ${formatted}</li>`;
  }, "");

  return `<ul>${items}</ul>`;
}

function formatParamValue(value) {
  if (_.isString(value) && value.length > 80) {
    return _.escape(value.substr(0, 80)) + "&hellip;";
  } else {
    return _.escape(value.toString());
  }
}
%>
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="<%- context.asAbsolutePath('assets/style.css') %>" />
</head>
<body>
  <h1>ROS Master Status</h1>

  <% if (status) { %>
    <h2>Parameters</h2>

    <div id="params">
      <%= formatParams(params) %>
    </div>

    <h2>Topics</h2>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Publishers</th>
          <th>Subscribers</th>
        </tr>
      </thead>
      <tbody>
        <% for (const name of _.union(Object.keys(publishers), Object.keys(subscribers))) { %>
          <tr>
            <td><%- name %></td>
            <td><%- _.has(publishers, name) ? publishers[name].join(", ") : "" %></td>
            <td><%- _.has(subscribers, name) ? subscribers[name].join(", ") : "" %></td>
          </tr>
        <% } %>
      </tbody>
    </table>

    <h2>Services</h2>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Providers</th>
        </tr>
      </thead>
      <tbody>
        <% for (const [name, providers] of _.pairs(services)) { %>
          <tr>
            <td><%- name %></td>
            <td><%- services[name].join(", ") %></td>
          </tr>
        <% } %>
      </tbody>
    </table>
  <% } else { %>
    <p>The ROS master is not running.</p>
  <% } %>
</body>
</html>
